/*
 * Copyright(c) 2020-2100 万达信息股份有限公司 版权所有
 * WONDERS INFORMATION CO., LTD. ALL RIGHTS RESERVED.
 */

package com.wondersgroup.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 自定义过滤器
 *
 * @author: huym
 * @version: 1.0
 * @date: 2020/11/20
 */
@Configuration
@Slf4j
public class CustomGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (exchange.getRequest().getQueryParams().get("username")!=null){
            log.info("用户身份合法，放行请求继续执行！！！");
            return chain.filter(exchange);
        }
        log.info("非法用户，拒绝访问！！！");
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() { // 数字越小filter越先执行
        return -1;
    }
}
